<?php
/**
 * Created by PhpStorm.
 * User: ou
 * Date: 2018/6/6
 * Time: 11:01
 */

namespace app\mogosdk\model;
use think\Model;
use \Firebase\JWT\JWT;
use think\Config;
use EasyWeChat\Factory;
use plugins\GeTui\GeTui;//需要自己下载
use Omnipay\Omnipay; //需要自己composer下载
// 加载配置
Config::load(APP_PATH.'mogosdk/config.php');

class Api extends Model{
    /**
     * easyWechat 实例化
     * @return \EasyWeChat\OfficialAccount\Application
     */
    protected function easyWeChat(){
        $Config=\config('Easywechat');
        return Factory::officialAccount($Config);
    }

    /**
     * 个推实例化
     * @param string $type  个推的实例化对象(因APP经常性两个端 例技术端、用户端)，默认 user 用户端
     * @return GeTui
     */
    protected function geTui($type = 'user'){
        $Config=\config('Igetui')[$type];
        return new GeTui($Config);
    }

    /**
     * 发送阿里云验证码 或者 通知
     * @param string $type 配置的参数类型(即键)
     * @param string $mobile 手机号码
     * @param array $data   模板变量
     * @return mixed
     */
    protected function aliyunSms($type = 'code',$mobile = '',$data = array()){
        $Config=\config('AliyunSms')[$type];
        return plugin_action('AliyunSms', 'AliyunSms', 'sendSms2', [$Config['SignName'],$Config['templateCode'] ,$mobile,$data]);
    }

    /**
     * 支付宝APP支付
     * @param string $notifyUrl  回调地址
     * @param string $body  回调用参数  例如 人人商城的 uniacid
     * @param string $title 商品名字
     * @param string $outTradeNo    订单号
     * @param null $money   交易金额
     * @return mixed
     */
    protected function alipay($notifyUrl = '',$body = '',$title = '',$outTradeNo = '',$money =NULL){
        $Config=\config('Alipay');
        $gateway = Omnipay::create('Alipay_AopApp');
        $gateway->setSignType('RSA2'); //RSA/RSA2
        $gateway->setAppId($Config['AppId']);
        $gateway->setPrivateKey($Config['PrivateKey']);
        $gateway->setAlipayPublicKey($Config['AlipayPublicKey']);
        $gateway->setNotifyUrl($notifyUrl);
        $request = $gateway->purchase();
        $request->setBizContent([
            'body' =>$body,
            'subject'      => $title,//商品名称
            'out_trade_no' =>$outTradeNo,//订单号
            'total_amount' => $money,//商品价格
            'product_code' => 'QUICK_MSECURITY_PAY',
        ]);

        /**
         * @var AopTradeAppPayResponse $response
         */
        $response = $request->send();

        $orderString = $response->getOrderString();

        return $orderString;
    }
}